package com.aaa.controller;

import com.aaa.entity.ReturnData;
import com.aaa.service.StudentService;
import com.aaa.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/23 10:27
 * @description：报名人员业务控制器
 * @modified By：
 * @version: 1.0
 */

@WebServlet(urlPatterns = "/student")
public class StudentController  extends BaseController {
    StudentService studentService= new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("========================");
        //调用父类的通用参数初始化方法
        Map<String, String> map = super.initParam(req, resp);
        //判断方法名
        String method=map.get("method")+"";
        ReturnData returnData = new ReturnData();
        switch (method){
            //将要去用户报名页面
            case "toAddStudentPage":
                //在次处从数据库获取该活动的起始时间，计划报名人数，实际报名人数，活动名称,活动状态
                Map<String, Object> businessInfo = studentService.getBusinessInfo(map);
                //如果将businessInfo带去jsp页面
                req.getSession().setAttribute("businessInfo",businessInfo);
                //页面跳转
                req.getRequestDispatcher("addStudent.jsp").forward(req,resp);
                break;
            case "addStudent":
                //添加报名人员信息
                //保存数据,然后跳转到报名成功页面
                boolean addStudent = studentService.addStudent(map);
                if(addStudent){
                    req.getRequestDispatcher("success.jsp").forward(req,resp);
                }else
                {
                    req.getRequestDispatcher("error.jsp").forward(req,resp);
                }
                break;
            case "findAllStudent":
                List<Map<String, Object>> allStudent = studentService.findAllStudent(map);
                //回传json数据

                returnData.setCode(0);
                //获取数据库表的总条数
                int count = studentService.getCount(map);
                returnData.setCount(count);
                returnData.setData(allStudent);
                //调用父类的通用方法
                super.toJsonString(returnData,resp);
                break;
            default:
                System.out.println("参数错误");
                break;
        }
    }
}
